package com.arelikebrothers.demo.concurrent.begin;

/**
 * Created by lennon on 01/05/2017.
 * 加锁不当，一个隐晦的bug
 */
public class BadlockOnInteger implements Runnable{
    public static Integer i = 0;

    static BadlockOnInteger instance = new BadlockOnInteger();


    @Override
    public void run() {
        for (int j = 0; j < 100000; j++) {
            synchronized (i) {
                i++;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(BadlockOnInteger.instance);
        Thread t2 = new Thread(BadlockOnInteger.instance);

        t1.start();
        t2.start();

        t1.join();
        t2.join();
        System.out.println(i);
    }
}
